package Mylinkedlist;

import LinkedList.Linkedlist;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: adol book
 * Date: 2022-05-02
 * Time: 12:40
 */
public class TestDemo {
    public static class LinkNode{
        public int val;
        public LinkNode next;

        public LinkNode(int val){
            this.val = val;
        }
    }
    public LinkNode head;
    public void creatlink(){
        LinkNode linkNode1 = new LinkNode(23);
        LinkNode linkNode2 = new LinkNode(24);
        LinkNode linkNode3 = new LinkNode(26);
        LinkNode linkNode4 = new LinkNode(45);
        LinkNode linkNode5 = new LinkNode(78);
        linkNode1.next = linkNode2;
        linkNode2.next = linkNode3;
        linkNode3.next = linkNode4;
        linkNode4.next = linkNode5;
        this.head = linkNode1;
    }
    public void display(){
        LinkNode cur = this.head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
    //头插法
    public void addFirst(int data){
        LinkNode node = new LinkNode(data);
        node.next = this.head;
        this.head = node;
    }

    //尾插法
    public void addLast(int data) {
        LinkNode node = new LinkNode(data);
        LinkNode cur = this.head;
        if (this.head == null) {
            this.head = node;
        } else {
            while (cur.next != null) {
               cur = cur.next;
            }
            cur.next = node;
        }
    }
    private void checkIndex(int index){
        if(index < 0 || index > size()){
            throw new addINdexEcxptiom("插入元素时，index下标不合法");
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        checkIndex(index);
        if(index == 0){
            addFirst(data);
            return;
        }
        if(index == size()){
            addLast(data);
            return;
        }
        LinkNode node = new LinkNode(data);
        LinkNode cur = findIndexfrist(index);
        node.next = cur.next;
        cur.next = node;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        LinkNode cur = this.head;
        while (cur != null){
            if(cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    private LinkNode findIndexfrist(int index){
        LinkNode cur = this.head;
        if(index-1 != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null){
            throw new headEcxption("删除指定位置时，链表为空");
        }
        LinkNode cur = this.head;
        if(this.head.val == key){
            this.head = this.head.next;
        }
        while (cur != null){
            if(cur.next.val == key){
                LinkNode del = cur.next;
                cur.next = del;
                return;
            }
            cur = cur.next;
        }
    }
    //删除所有值为key的节点
    public void removeAllKey(int key){
        if(this.head == null){
            return;
        }
        LinkNode cur = this.head.next;
        LinkNode prev = this.head;
        while (cur != null){
            if(cur.val == key){
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        if(this.head.val == key){
              head = head.next;
        }
    }
    //得到单链表的长度
    public int size(){
        int count = 0;
        LinkNode cur = this.head;
        while (cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }
    public void clear(){
        LinkNode cur = this.head;
        LinkNode curNEXT = null;
        while (cur != null){
            curNEXT = cur.next;
            cur.next = null;
            cur = curNEXT;
        }
        this.head = null;
    }
}
